﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Core.Interfaces;
using Company.Shell.Dialogs;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using Company.Shell.Services;
using HandyControl.Controls;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    internal class ConfigViewModel : ObservableObject
    {
        public ICommand? SaveCommand { get; }
        private IConfigManager ConfigManager { get; }
        public IConfigProvider ConfigProvider { get; }
        public IRoomManager RoomManager { get; }
        public Config Config { get; }
        public ICommand? AddAlarmItemCommand { get; }
        private AlarmItem alarmItem = new AlarmItem();
        public AlarmItem AlarmItem
        {
            get=> alarmItem;
            set=>SetProperty(ref  alarmItem, value);
        }
        public ConfigViewModel(
            IRoomManager roomManager,
            IConfigManager configManager, 
            IConfigProvider configProvider)
        {
            RoomManager = roomManager;
            ConfigManager = configManager;
            ConfigProvider = configProvider;
            Config = configProvider.Config;
            SaveCommand = new RelayCommand(OnSaveCommand);
            AddAlarmItemCommand = new RelayCommand(OnAddAlarmItemCommand);
        }

        private void OnAddAlarmItemCommand()
        {
            if (string.IsNullOrEmpty(AlarmItem.Address))
            {
                Dialog.Show(new MessageDialog("请输出报警地址和位"));
                return;
            }
            if (string.IsNullOrEmpty(AlarmItem.Description))
            {
                Dialog.Show(new MessageDialog("请输出报警描述"));
                return;
            }
            ConfigProvider.AlarmItems.Add(AlarmItem);
            AlarmItem = new AlarmItem();
        }

        private void OnSaveCommand()
        {
            ConfigManager.Write(Core.Enums.ConfigType.System, Config);
            ConfigManager.Write(Core.Enums.ConfigType.Alarm, ConfigProvider.AlarmItems);
            Dialog.Show<MessageDialog>();
        }
    }
}
